const sysuserService = require('../service/sysuser');
const retCode = require('../utils/retcode')
class SysUserController {
  async profile(ctx) {
    const res = await userService.profile();
    ctx.body = res.data;
  }

  /**
   * 登录
   * @param  {object} ctx   上下文
   * @return {object}       结果
   */
  async login ( ctx ) {
    let form = ctx.request.body

    const args = {
        username: form.username,
        userpass: form.password
    }

    let result = {
        code: retCode.Success,
        data: null
    }

    //验证非空
    if(!args.username || !args.userpass){
        ctx.body = {
          status: retCode.ArgsError,
          statusText: '参数错误',
          currentAuthority: 'guest',
        };
    } else {
      //根据用户名得到用户信息
      let userResult = await sysuserService.getByUserName(args)

      //用户不存在
      if(userResult.data.length == 0){

          ctx.body = {
            status: retCode.UserNotExist ,
            statusText: '用户不存在',
            currentAuthority: 'guest',
          };
      } else if (userResult.data[0].login_name != args.username || userResult.data[0].password != args.userpass){  //用户名或密码错误
          ctx.body = {
            status: retCode.UsernameOrPasswordError ,
            statusText: '用户名或者密码错误',
            currentAuthority: 'guest',
          };

      }  else {

        //将用户ID存入Session中
        ctx.session = {id: userResult.data[0].id}

        ctx.body = {
          status: retCode.Success,
          statusText: '成功',
          currentAuthority: userResult.data[0].id,
          userInfor:userResult.data[0]
        };
      }
    }
  }

   /**
   * 注册
   * @param  {object} ctx   上下文
   * @return {object}       结果
   */
  async register ( ctx ) {
    let form = ctx.request.body

    const args = form.values;

    //验证非空
    if(!args.login_name || !args.e_mail || !args.password || !args.user_name ||  !args.org_level){
        ctx.body = {
          status: retCode.ArgsError  ,
          statusText: '参数错误',
          currentAuthority: '0',
        };
    }else{
      //根据用户名得到用户数量
      let loginNameNumResult = await sysuserService.getCountByLoginName(args)

      //用户名已被注册
      if(loginNameNumResult.data[0].result_count > 0){
        ctx.body = {
          status: retCode.UserExisted  ,
          statusText: '用户已经存在',
          currentAuthority: '0',
        };
      }else{
        //插入注册数据
        let userResult = await sysuserService.add(args)
        if(userResult.insertId <= 0){
            ctx.body = {
              status: retCode.Fail  ,
              statusText: '插入注册数据失败',
              currentAuthority: '0',
            };
        }else{
          ctx.body = {
            status: retCode.Success,
            statusText: '插入注册数据成功',
            currentAuthority: 'user',
          };
        }
      }
    }
  }

  /**
   * 修改口令 (登录状态下修改密码)
   * @param {*} ctx
   */
  async resetPasswd ( ctx ) {

    let form = ctx.request.body
    if(form.values.password == 'RESET'){
      form.values.password = '12345678';
    }

    let resultCount = await sysuserService.setUserPasswdById(form.values)
    if(resultCount.data.affectedRows > 0 ){
      ctx.body = {
        status: retCode.Success  ,
        statusText: '修改口令成功',
        currentAuthority: 'user',
      };
    }else{
      ctx.body = {
        status: retCode.Fail  ,
        statusText: '修改口令失败',
        currentAuthority: '0',
      };

    }


  }

  /**
   * 根据用户Id修改用户信息
   * @param {} ctx
   */
  async setUserInfo ( ctx ) {
    let form = ctx.request.body

    let resultCount = await sysuserService.setUserById(form.values)
    if(resultCount < 1) {
        //修改失败
        ctx.body = {
          status: retCode.Success  ,
          statusText: '修改用户信息失败',
          currentAuthority: '0',
        };
    }else{
      //修改成功
        ctx.body = {
        status: retCode.Success  ,
        statusText: '修改用户信息成功',
        currentAuthority: '0',
      };
    }






  }

  async logout(ctx) {
    ctx.body = {
      status: 200,
      statusText: 'ok',
      currentAuthority: 'guest',
    };
  }

   /**
   * 分页查询
   * @param  {object} ctx   上下文
   * @return {object}       结果
   */

  async userFilter(ctx) {
    let form = ctx.request.body

    let pageIndex = parseInt(form.page)
    let limit = parseInt(form.pageSize)
    let offset = (pageIndex -1)* limit
    const args = {
      value:    form.value,
      limit:    limit,
      offset:   offset
    }

    //用户信息List
    let userListResult = await sysuserService.getUserListByFilter(args)
    let totalResult = await sysuserService.getUserCountByFilter(args)

    let total = totalResult[0].total
    ctx.body = {
      status: retCode.Success,
      statusText: '查询成功',
      data:{
        table:userListResult,
        pagination: {
          pageIndex: pageIndex,
          recordTotal: total,
          pageSize: limit,
        },
      },
    }


  }
  async mockdata(ctx){
    ctx.body =  {
      success: true,
      message: "T1108泰福高正品不锈钢真空保温壶保温瓶水壶暖",
      data: {
        foo: "DJ美国百丽耐热"
      }
    };
  }
  async yearFilter(ctx) {
    let form = ctx.request.body
    let pageIndex = parseInt(form.page)
    let limit = parseInt(form.pageSize)
    let offset = (pageIndex -1)* limit
    const args = {
      ymonth: form.ymonth,
      devicetype: form.devicetype,
      limit: limit,
      offset: offset
    }

    //List
    let data = await userService.getYearListByFilter(args)

    let totalResult = await userService.getYearCountByFilter(args)
    let total = totalResult[0].total
    ctx.body = {
      status: retCode.Success,
      statusText: '查询成功',
      data:{
        table:data,
        pagination: {
          pageIndex: pageIndex,
          recordTotal: total,
          pageSize: limit,
        },
      },
    }

  }
}

module.exports = new SysUserController();
